Dashboard Temp Share Shortlinks Frames API

HTMLify

287. Find the Duplicate Number.java
Views: 1 | Author: cody
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// 287. Find the Duplicate Number java solution

class Solution {
    public int findDuplicate(int[] nums) {
        int n= nums.length;
        HashMap<Integer,Integer> map = new HashMap<>();

        for(int i:nums){
            map.put(i,map.getOrDefault(i,0)+1);
        }
        for(int i:map.keySet()){
            if(map.get(i)>1)
            return i;
        }
        return -1;
    }
}